package com.lpb.spring.lpbspring.getway.rtc;

import com.google.common.collect.Maps;
import com.lpb.spring.lpbspring.getway.rtc.enums.EnumServiceSpecification;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author linpengbo
 * @date 2023/3/1
 */
public class TrtcRecordUsageVo {

    private List<RecordUsageVo> recordingUsageList;

    public List<RecordUsageVo> getRecordingUsageList() {
        return recordingUsageList;
    }

    public void setRecordingUsageList(List<RecordUsageVo> recordingUsageList) {
        this.recordingUsageList = recordingUsageList;
    }


    public static Map<EnumServiceSpecification,Long> filterUsageByTimeInterval(String dateHour, TrtcRecordUsageVo usageVo){
        Map<EnumServiceSpecification,Long> resultMap= Maps.newHashMap();
        if(usageVo==null|| CollectionUtils.isEmpty(usageVo.getRecordingUsageList())){
            return  resultMap;
        }
        String date = dateHour.substring(0, 8);
        String hour = dateHour.substring(8, 10);
        String dF = StringUtils.join(date.substring(0,4),"-",date.substring(4,6),"-",date.substring(6,8)," "+hour);
        List<RecordUsageVo> usageList = usageVo.getRecordingUsageList();
        for (RecordUsageVo u:usageList) {
            if( u.getTimeKey().indexOf(dF)!=0){
                continue;
            }
            if(resultMap.containsKey(EnumServiceSpecification.AUDIO)){
                resultMap.put(EnumServiceSpecification.AUDIO,resultMap.get(EnumServiceSpecification.AUDIO)+u.getAudioTime());
            }else{
                if(u.getAudioTime()!=null&&u.getAudioTime().longValue()>0){
                    resultMap.put(EnumServiceSpecification.AUDIO,u.getAudioTime());
                }
            }

            if(resultMap.containsKey(EnumServiceSpecification.SD)){
                resultMap.put(EnumServiceSpecification.SD,resultMap.get(EnumServiceSpecification.SD)+u.getClass1VideoTime());
            }else{
                if(u.getClass1VideoTime()!=null&&u.getClass1VideoTime().longValue()>0){
                    resultMap.put(EnumServiceSpecification.SD,u.getClass1VideoTime());
                }
            }
        }
        return resultMap;
    }



    static class RecordUsageVo{
        private String timeKey;

        /**
         * 语音时长
         */
        private Long audioTime;
        /**
         * 视频时长-标清SD
         */
        private Long  class1VideoTime;
        /**
         * 视频时长-高清hd
         */
        private Long class2VideoTime;
        /**
         * 视频时长-超高清hd
         */
        private Long class3VideoTime;

        public String getTimeKey() {
            return timeKey;
        }

        public void setTimeKey(String timeKey) {
            this.timeKey = timeKey;
        }

        public Long getAudioTime() {
            return audioTime;
        }

        public void setAudioTime(Long audioTime) {
            this.audioTime = audioTime;
        }

        public Long getClass1VideoTime() {
            return class1VideoTime;
        }

        public void setClass1VideoTime(Long class1VideoTime) {
            this.class1VideoTime = class1VideoTime;
        }

        public Long getClass2VideoTime() {
            return class2VideoTime;
        }

        public void setClass2VideoTime(Long class2VideoTime) {
            this.class2VideoTime = class2VideoTime;
        }

        public Long getClass3VideoTime() {
            return class3VideoTime;
        }

        public void setClass3VideoTime(Long class3VideoTime) {
            this.class3VideoTime = class3VideoTime;
        }
    }


}
